package com.wb.mas.widget;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.wb.mas.R;

import java.util.List;

import me.goldze.mvvmhabit.widget.BottomPushPopupWindow;
import me.goldze.mvvmhabit.widget.WheelView;

/**
 * Copyright (C) 2018,
 * SelectPopWindow
 * <p>
 * Description
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-05-05, mwl, Create file
 */
public class SelectPopWindow extends BottomPushPopupWindow<List<String>> {
    private OnPopitemClickLinstener popitemClickLinstener;
    private WheelView wheelView;
    private List<String> datas;
    private String titleStr;
    private TextView tvTitle;

    public SelectPopWindow(Context context, List<String> strings, String title) {
        super(context, strings);
        titleStr = title;
        datas = strings;
        tvTitle.setText(titleStr);
    }

    @Override
    protected View generateCustomView(List<String> strings) {
        View root = View.inflate(context, R.layout.pop_select, null);
        TextView tvCancel = root.findViewById(R.id.tv_cancel);
        TextView tvConfirm = root.findViewById(R.id.tv_confirm);
        tvTitle = root.findViewById(R.id.tv_title);

        wheelView = root.findViewById(R.id.wv_select);
        wheelView.setOffset(2);
        wheelView.setItems(strings);
        wheelView.setSeletion(0);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvTitle.setText(titleStr);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popitemClickLinstener != null) {
                    popitemClickLinstener.OnItemClick(titleStr, wheelView.getSeletedIndex());
                }
            }
        });
        return root;
    }


    public void setOnPoPItemClickLinstener(OnPopitemClickLinstener clickLinstener) {
        this.popitemClickLinstener = clickLinstener;
    }


    public void show(Activity activity, String selectStr) {
        super.show(activity);
        int select = datas.indexOf(selectStr);
        if (select != -1) {
            wheelView.setSeletion(select);
        }
    }

    public void show(Activity activity, int position) {
        super.show(activity);
        if (position != -1) {
            wheelView.setSeletion(position);
        }
    }
}